#!/usr/local/bin/rake
# Copyright (C) 2003-2010 Wang Pengcheng <wpc0000@gmail.com>
# , released under the MIT license
# You may obtain a template of the MIT License at
# http://www.opensource.org/licenses/mit-license.php
$KCODE = 'utf8'

require 'fileutils'
require 'yaml'

def confirm(msg)
  puts msg+'[y/n]?'
  f = STDIN.gets
  exit unless /y(es)?/i=~f
end

CONF= YAML.load_file('.auths.yml')
USER = CONF['user']
PASSWD = CONF['passwd']
DEFALUT_SUM_LASTEST = 'Latest version of Chrome terminal framework extension'
DEFALUT_CRX= './'+'ctf-extension-latest-version.crx'

#Default message for hg
DEFALUT_MSG = 'Format the sources'
GOOGLE_SERVER='ctf-extension.googlecode.com'
SCRIPT='script/'
require SCRIPT+'crxmake'

PUSH = SCRIPT+'push'
UPLOAD = SCRIPT+'googlecode_upload.py'

task :default => :ci

desc 'Commit sources'
task :ci,:msg do |t,args|
  args.with_defaults(:msg => DEFALUT_MSG)
  confirm('Commit with message: '+args.msg) if DEFALUT_MSG==args.msg
  # Push source
  sh PUSH,args.msg,USER,PASSWD, GOOGLE_SERVER
end

desc 'Upload the last version of extension'
task :new do
  confirm('Upload the crx file, please Deprecated it,http://code.google.com/p/ctf-extension/downloads/delete?name=ctf-extension-latest-version.crx ')
  # Packaging the crx
  CrxMake.make(
    :ex_dir => "./lib",
    :pkey => './doc/key.pem',
    :crx_output => DEFALUT_CRX,
    :ignorefile => /\.swp/
  )

  # Upload the lastest version crx
  sh UPLOAD, '--user='+USER, '--password='+PASSWD,
    '--project=ctf-extension',
    '--summary='+DEFALUT_SUM_LASTEST, '--labels=OpSys-All,Type-Extension',
    DEFALUT_CRX
  FileUtils.rm DEFALUT_CRX
end

